package com.xunqi.permission.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xunqi.permission.common.JsonData;
import com.xunqi.permission.dto.AclModuleLevelDto;
import com.xunqi.permission.param.RoleParam;
import com.xunqi.permission.pojo.SysRole;
import com.xunqi.permission.pojo.SysUser;
import com.xunqi.permission.service.*;
import com.xunqi.permission.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Api(tags = "角色控制器")
@Controller
@RequestMapping(value = "/sys/role")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysTreeService sysTreeService;
    @Resource
    private SysRoleAclService sysRoleAclService;
    @Resource
    private SysRoleUserService sysRoleUserService;
    @Resource
    private SysUserService sysUserService;

    @ApiOperation(value = "跳转到角色页面",httpMethod = "GET",protocols = "HTTP",produces = "role.page",response = String.class)
    @RequestMapping(value = "role.page")
    public String page() {
        return "role";
    }


    @ApiOperation(value = "角色新增功能",httpMethod = "POST",protocols = "HTTP",produces = "/save.json",response = JsonData.class)
    @RequestMapping(value = "/save.json")
    @ResponseBody
    public JsonData saveRole(RoleParam param) {
        sysRoleService.save(param);
        return JsonData.success();
    }

    @ApiOperation(value = "角色修改功能",httpMethod = "POST",protocols = "HTTP",produces = "/update.json",response = JsonData.class)
    @RequestMapping(value = "/update.json")
    @ResponseBody
    public JsonData updateRole(RoleParam param) {
        sysRoleService.update(param);
        return JsonData.success();
    }

    @ApiOperation(value = "获取所有角色列表",httpMethod = "GET",protocols = "HTTP",produces = "/list.json",response = JsonData.class)
    @RequestMapping(value = "/list.json")
    @ResponseBody
    public JsonData listRole() {
        List<SysRole> list = sysRoleService.getAll();
        return JsonData.success(list);
    }


    @ApiOperation(value = "角色权限数展示列表",httpMethod = "GET",protocols = "HTTP",produces = "/roleTree.json",response = JsonData.class)
    @RequestMapping(value = "/roleTree.json")
    @ResponseBody
    public JsonData roleTree(@RequestParam("roleId") int roleId) {
        List<AclModuleLevelDto> levelDtos = sysTreeService.roleTree(roleId);
        return JsonData.success(levelDtos);
    }

    @ApiOperation(value = "/角色与权限的关系维护",httpMethod = "GET",protocols = "HTTP",produces = "/changeAcls.json",response = JsonData.class)
    @RequestMapping(value = "/changeAcls.json")
    @ResponseBody
    public JsonData changeAcls(@RequestParam("roleId") int roleId, @RequestParam(value = "aclIds", required = false, defaultValue = "") String aclIds) {
        List<Integer> aclIdList = StringUtil.splitToListInt(aclIds);
        sysRoleAclService.changeRoleAcls(roleId, aclIdList);
        return JsonData.success();
    }

    @ApiOperation(value = "/角色与用户的关系维护",httpMethod = "GET",protocols = "HTTP",produces = "/changeUsers.json",response = JsonData.class)
    @RequestMapping("/changeUsers.json")
    @ResponseBody
    public JsonData changeUsers(@RequestParam("roleId") int roleId, @RequestParam(value = "userIds", required = false, defaultValue = "") String userIds) {
        List<Integer> userIdList = StringUtil.splitToListInt(userIds);
        sysRoleUserService.changeRoleUsers(roleId, userIdList);
        return JsonData.success();

    }


    @ApiOperation(value = "角色与用户模块交互功能的实现",httpMethod = "GET",protocols = "HTTP",produces = "/users.json",response = JsonData.class)
    @RequestMapping(value = "/users.json")
    @ResponseBody
    public JsonData users(@RequestParam("roleId") int roleId) {
        List<SysUser> selectedUserList = sysRoleUserService.getListByRoleId(roleId);
        List<SysUser> allUserList = sysUserService.getAll();
        List<SysUser> unselectedUserList = Lists.newArrayList();

        //加速遍历list集合
        Set<Integer> selectedUserIdSet = selectedUserList.stream().map(sysUser -> sysUser.getId()).collect(Collectors.toSet());
        for (SysUser sysUser : allUserList) {
            if (sysUser.getStatus() == 1 && !selectedUserIdSet.contains(sysUser.getId())) {
                unselectedUserList.add(sysUser);
            }
        }
        //状态不等于1的人进行过滤操作
        //selectedUserList = selectedUserList.stream().filter(sysUser -> sysUser.getStatus() != 1).collect(Collectors.toList());
        Map<String,List<SysUser>> map = Maps.newHashMap();
        map.put("selected",selectedUserList);
        map.put("unselected",unselectedUserList);
        return JsonData.success(map);
    }
}
